﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace LinearAlgebra
{
    static class UtilityMethods
    {
        private static Keys[] acceptableKeys = { Keys.Back, Keys.Escape, Keys.Delete, Keys.Home, Keys.End, Keys.Up, Keys.Down, Keys.Left, Keys.Right };
        private static Random rng = new Random();

        public static bool ValidateFloatKeyEvent(TextBox textBox, KeyEventArgs keyEvent)
        {
            int MAX_LENGTH = 4;

            if (!(textBox.SelectionLength > 0) && textBox.Text.Length > MAX_LENGTH && !acceptableKeys.Contains(keyEvent.KeyCode))
            {
                int numDigits = 0;

                foreach (char character in textBox.Text)
                {
                    if (Char.IsDigit(character))
                        numDigits++;
                }

                if (numDigits > MAX_LENGTH)
                    keyEvent.SuppressKeyPress = true;
            }


            if (!Char.IsDigit((char)keyEvent.KeyValue) &&
                keyEvent.KeyCode != Keys.OemPeriod &&
                keyEvent.KeyCode != Keys.OemMinus &&
                !acceptableKeys.Contains(keyEvent.KeyCode))
                keyEvent.SuppressKeyPress = true;

            if (keyEvent.KeyCode == Keys.OemPeriod && textBox.Text.Contains('.'))
                keyEvent.SuppressKeyPress = true;
            if (keyEvent.KeyCode == Keys.OemMinus && textBox.Text.Length > 0)
                keyEvent.SuppressKeyPress = true;

            return (keyEvent.KeyCode == Keys.Escape);
        }
        static public string GetRandomNumberString(int maxAbsoluteValue)
        {
            return (rng.Next(maxAbsoluteValue) * 2 * (rng.NextDouble() - 0.5)).ToString("0.00", CultureInfo.InvariantCulture); // a real number between -20 and 20
        }
    }
}
